<?php
/**
 * 《产品质检》实现类
 *
 * @author 孙晓晔
 * @version $Id: Quality.class.php 1054 2010-04-07 11:36:03Z sunxy $
 */
require_once(dirname(__FILE__) . '/MoldUtil.class.php');

class Quality extends MoldUtil {

	var $info_dir = 'quality';

	public $file = '4';

	public $tbl = Constant::tbl_mold_quality;

	public $status = array(
		'-' => '请选择',

		'1' => '异常',
		'9' => '合格',
	);

	public $groupby = array(
		'-' => '请选择',
		'mold' => '产品',
		'emp' => '负责人',
		'status' => '状态',
		'1' => '日期',
		'2' => '月份',
	);

	public $orderby = array(
		'-' => '请选择',
		'mold' => '产品',
		'cost' => '费用',
		'emp' => '负责人',
		't2' => '完成日期',
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->assign('t1', strtotime('-3 day'));

		$controller->display($request, 'mold/quality/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		
		$piece_s = $request['piece_s'];
		$technics_s = $request['technics_s'];

		$where = " WHERE 1=1";

		if (!empty($piece_s) && $piece_s <> '-') {
			$where .= " AND piece = '" . $piece_s . "'";
		}

		if (!empty($technics_s) && $technics_s <> '-') {
			$where .= " AND technics = '" . $technics_s . "'";
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$produce = $this->id(Constant::tbl_mold_produce, $row['produce']);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}
			
		if ($this->_is_admin()) {
			$controller->display($request, 'mold/quality/admin/list');
		} else {
			$controller->display($request, 'mold/quality/list');
		}
		return true;
	}

	/**
	 * 添加质检报告
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		$row = $this->id(Constant::tbl_mold_produce, $id);

		$row['piece'] = $this->piece[$row['piece']];
		$row['technics'] = $this->technics[$row['technics']];

		$time = $row['tt'];

		$row['hour'] = floor($time / 60);
		$row['minute'] = $time % 60;

		$row['emp_t'] = Name::emp($row['emp']);

		$controller->assign_by_ref('result', $row);

		$controller->assign_by_ref('emp_option', $this->id_option(Constant::tbl_emp, 'all'));

		require_once 'Mold.class.php';
		$mold = new Mold();
		$controller->assign_by_ref('mold', $mold->_view($row['mold']));

		$this->time($controller);

		$controller->assign('t1', strtotime('-1 day'));

		$controller->display($request, 'mold/quality/add');
		return true;
	}

	/**
	 * 添加
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		$produce = $this->id(Constant::tbl_mold_produce, $id);

		$content = addslashes($request['content']);
		$exception = addslashes($request['exception']);

		$date = $request['date'];

		$status = $request['status'];

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(produce, mold, piece, technics, content, exception, date, emp, status, createtime) ";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now())";
		$sql = sprintf($fmt, $this->tbl, $id, $produce['mold'], $produce['piece'], $produce['technics'], $content, $exception, $date, $_SESSION['id'], $status);
		// debug($sql);
		$db->query($sql);

		$ret = array('id'=> $db->insert_id());

		$this->produce($id, $status);
			
		echo json_encode($ret);
		return true;
	}

	function produce($id, $status) {
		$aa = array('1'=>'8', '9'=>'9');

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET status = '%s' WHERE id = '%s'";
		$sql = sprintf($fmt, Constant::tbl_mold_produce, $aa[$status], $id);
		// debug($sql);
		$db->query($sql);

		clean(Constant::tbl_mold_produce . "_id_" . $id);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$mold = $this->id(Constant::tbl_mold, $row['mold']);
		$mold['engineer_t'] = Name::emp($mold['engineer']);

		$controller->assign_by_ref('mold', $mold);

		$controller->display($request, 'mold/quality/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$content = addslashes($request['content']);
		$exception = addslashes($request['exception']);
		$solution = addslashes($request['solution']);

		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET content = '%s', exception = '%s', solution = '%s'";
		$fmt .= ", status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $content, $exception, $solution, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);		

		$controller->assign_by_ref('result', $row);

		require_once 'Mold.class.php';
		$mold = new Mold();
		$controller->assign_by_ref('mold', $mold->_view($row['mold']));

		$controller->assign_by_ref('file', $this->_file($row));

		$controller->display($request, 'mold/quality/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['mold_t'] = Name::mold($row['mold']);
		
		$row['piece'] = $this->piece[$row['piece']];
		$row['technics'] = $this->technics[$row['technics']];

		$row['file'] = Download::url($this->info_dir, $row['createtime'], $row['produce'], $row['filename']);
		$row['filesize_t'] = printsize($row['filesize']);

		$row = $this->_view_t($row);
				
		return $row;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}

			$fmt = "delete from %s WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $id);
			// debug($sql);
			$db->query($sql);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function report($controller, $request) {
		$this->pretreat($controller);

		$mold_s = $request['mold_s'];
		$piece_s = $request['piece_s'];
		$technics_s = $request['technics_s'];

		$db = newdb($this->dsn_r);
		$sql = "SELECT * FROM " . $this->tbl;
		$where = " WHERE 1=1";

		if (!empty($mold_s)) {
			$where .= " AND mold = '" . $mold_s . "'";
		}

		if (!empty($piece_s) && $piece_s <> '-') {
			$where .= " AND piece = '" . $piece_s . "'";
		}

		if (!empty($technics_s) && $technics_s <> '-') {
			$where .= " AND technics = '" . $technics_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		$orderby = $this->orderby($request['r1']);
		$sql .= $where . $orderby;
		// debug($sql);
		$result = $db->query($sql);

		while ($row = $db->fetch_array($result)) {
			$row['mold_t'] = Name::mold($row['mold']);

			$produce = $this->id(Constant::tbl_mold_produce, $row['produce']);

			$row['piece'] = $this->piece[$produce['piece']];
			$row['technics'] = $this->technics[$produce['technics']];

			$row['file'] = Download::url($this->info_dir, $row['createtime'], $row['produce'], $row['filename']);
			$row['filesize_t'] = printsize($row['filesize']);

			$row['emp_t'] = Name::emp($row['emp']);

			$row['status_t'] = $this->status[$row['status']];

			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);

		$controller->display($request, 'mold/quality/report');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function group($controller, $request) {
		$this->pretreat_group($controller);

		$controller->assign('t1', strtotime('-1 month'));

		$controller->display($request, 'mold/quality/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'mold');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'mold') {
			$row['a'] = sprintf("%s [%s]", Name::mold($row['a']), $row['a']);
		} else {

			$row = $this->_name_t($row, $px);
		}

		return $row;
	}

	function _report_status($id) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT status, COUNT(status) c FROM %s WHERE mold = '%s' GROUP BY status";
		$sql = sprintf($fmt, $this->tbl, $id);
		// debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row['status_t'] = $this->status[$row['status']];

			$ret[] = $row;
		}

		return $ret;
	}
}
?>